//
//  XMGTopicPictureView.m
//  百思不得姐
//
//  Created by 杨庆 on 2017/1/15.
//  Copyright © 2017年 杨庆. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "XMGTopicPictureView.h"
#import "XMGTopic.h"
#import "UIImageView+WebCache.h"//网络图片
#import "XMGProgressView.h"
#import "XMGShowPictureViewController.h"
@interface XMGTopicPictureView ()
/**GIF 标识*/
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
/**图片*/
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/**查看大图按钮*/
@property (weak, nonatomic) IBOutlet UIButton *seeBigPicture;
/** 进度条 */
@property (weak, nonatomic) IBOutlet XMGProgressView *progressView;


@end


@implementation XMGTopicPictureView

+(instancetype)pictureView{
	
	return [[[NSBundle mainBundle ]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
	
}


-(void)awakeFromNib{
	[super awakeFromNib];
	self.autoresizingMask = UIViewAutoresizingNone;
	
	
	// 给图片添加监听器(手势)
	self.imageView.userInteractionEnabled=YES;  //可点击
	[self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPicture)]];

	
}







-(void)setTopic:(XMGTopic *)topic{
	_topic=topic;
	
	/**
	  *1. SDWebImage 通过取出图片的第一个字节(存储图片类型数据的)来片段图片的类型
	  *2. 拿出图片的扩展名, 如果是 gif 就是动态图
	 */
	
	// 立马显示最新的下载进度(防止显示的是其他图片下载进度)
	[self.progressView setProgress: topic.pictureProgress animated:NO];
	
	// 设置图片
	[self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.large_image] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
		self.progressView.hidden = NO; // 循环利用时需要显示回来
		// 计算进度值
		topic.pictureProgress =1.0* receivedSize/expectedSize;
		// 显示进度值
		[self.progressView setProgress:topic.pictureProgress animated:NO];
	
	} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
		self.progressView.hidden = YES;
		
		// 是大图才进行绘图处理
		if (topic.isBigPictuer ==NO) return ;
		/** 大图显示顶部
		 * 1.压缩至矩形框
		 * 2.图形上下文大小
		 */
		
		
		// 开启图形上下文
		UIGraphicsBeginImageContextWithOptions(topic.pictureF.size, YES, 0.0);
		
		// 下载完的 image 对象绘制到图形上下文
		CGFloat width = topic.pictureF.size.width;
		CGFloat height =width*image.size.height / image.size.width;
		
		[image drawInRect:CGRectMake(0, 0, width , height)];
		
		// 获得图片
		self.imageView.image=UIGraphicsGetImageFromCurrentImageContext();
		
		// 结束图形上下文
		UIGraphicsEndImageContext();
		
		
		
		
		
	}];
	
	// 一 . 判断是否为 gif
		// 1. 取出扩展名
		NSString*extension = topic.large_image.pathExtension;
		// 2. 如果不是gif格式就隐藏  (gif 格式播放时 SDWebImage 内部自动处理的过程)
		self.gifView.hidden = ![extension.lowercaseString isEqualToString:@"gif"];
	
	// 二. 判断显示按钮(超出最大高度[自定义] 就显示)
		if (topic.isBigPictuer) {// 大图
			self.seeBigPicture.hidden = NO;
			self.imageView.contentMode = UIViewContentModeScaleAspectFill;
		}else{ // 不是大图
			self.seeBigPicture.hidden = YES;
		}
}




-(void)showPicture{
	// 弹出控制器
	XMGShowPictureViewController*showPicture=[[XMGShowPictureViewController alloc]init];
	showPicture.topic=self.topic;
	
	[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicture animated:YES completion:^{
	
	}];

}
@end
